/*
 作者：李振超    2017年8月15日 09:49
 有哪些对象?

 有一个对象:三级联动
 */
var threeLevelChange = {
    //所有的初始化相关的动作: 默认显示北京都是信息, 给省市的select注册事件
    init: function (){
        //给当前对象添加属性
        this.allSelect = document.querySelectorAll("select");
        this.fillProvinces();
        this.fillCities(1);
        this.fillCounties(1);
        var self = this;
        this.allSelect[0].onchange = function (){
            var proID = this.value;   // dom事件处理程序中的this指代那个dom元素
            self.fillCities(proID);
            self.fillCounties(self.allSelect[1].value);
        }
        this.allSelect[1].onchange = function (){
            var cityID = this.value;
            self.fillCounties(cityID);
        }
    },
    fillProvinces: function (){
        var provinceList = data.provinceList;
        for (var province of provinceList){
            var proName = province.ProName;
            var proID = province.ProID;
            var opt = this.createOption(proName, proID);
            this.allSelect[0].appendChild(opt);
        }
    },
    fillCities: function (proID){
        this.allSelect[1].innerHTML = "";
        var cities = data.cityList;
        for (var city of cities){
            if (proID == city.ProID){
                var opt = this.createOption(city.CityName, city.CityID);
                this.allSelect[1].appendChild(opt);
            }
        }
    },
    fillCounties: function (cityID){
        this.allSelect[2].innerHTML = ""
        var counties = data.countyList;
        for (var county of counties){
            if (cityID == county.CityID){
                var opt = this.createOption(county.DisName, county.ID);
                this.allSelect[2].appendChild(opt);
            }
        }
    },
    createOption: function (text, value){
        var option = document.createElement("option");
        option.value = value;
        option.innerHTML = text;
        return option;
    }

}

threeLevelChange.init();